package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.service.IApplicantService;
import com.mdd.admin.validate.ApplicantCreateValidate;
import com.mdd.admin.validate.ApplicantUpdateValidate;
import com.mdd.admin.validate.ApplicantSearchValidate;
import com.mdd.admin.vo.ApplicantListedVo;
import com.mdd.admin.vo.ApplicantDetailVo;
import com.mdd.common.config.GlobalConfig;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.Applicant;
import com.mdd.common.mapper.ApplicantMapper;
import com.mdd.common.util.ListUtils;
import com.mdd.common.util.TimeUtils;
import com.mdd.common.util.UrlUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 申请人列实现类
 * @author LikeAdmin
 */
@Service
public class ApplicantServiceImpl implements IApplicantService {
        
    @Resource
    ApplicantMapper applicantMapper;

    /**
     * 申请人列列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<ApplicantListedVo>
     */
    @Override
    public PageResult<ApplicantListedVo> list(PageValidate pageValidate, ApplicantSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<Applicant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");

        applicantMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:customerName@customer_name:str",
            "=:applicantType@applicant_type:str",
            "=:customerNameChinese@customer_name_chinese:str",
            "=:applicantId@applicant_id:int",
            "=:streetChinese@street_chinese:str",
            "=:feeReductionFiling@fee_reduction_filing:str",
            "gte:reductionCreateTime@reduction_create_time:str",
            "lte:reductionEndTime@reduction_end_time:str",
            "=:nation:str",
            "=:isAnnualMonitoringFee@is_annual_monitoring_fee:str",
            "like:updateUserName@update_user_name:str",
            "datetime:createTimeStart-createTimeEnd@create_time:str",
        });

        IPage<Applicant> iPage = applicantMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<ApplicantListedVo> list = new LinkedList<>();
        for(Applicant item : iPage.getRecords()) {
            ApplicantListedVo vo = new ApplicantListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    @Override
    public List<ApplicantListedVo> all(ApplicantSearchValidate searchValidate) {
        QueryWrapper<Applicant> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.orderByDesc("id");
        if (searchValidate.getCustomerId()!=null){
            queryWrapper.eq("customer_id", searchValidate.getCustomerId());
        }

        applicantMapper.setSearch(queryWrapper, searchValidate, new String[]{
                "=:userId@user_id:int",
                "=:applicantType@applicant_type:str",
        });

        List<Applicant> iPage = applicantMapper.selectList(queryWrapper);

        List<ApplicantListedVo> list = new LinkedList<>();
        for(Applicant item : iPage) {
            ApplicantListedVo vo = new ApplicantListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }
        return list;
    }

    /**
     * 申请人列详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return Applicant
     */
    @Override
    public ApplicantDetailVo detail(Integer id) {
        Applicant model = applicantMapper.selectOne(
                new QueryWrapper<Applicant>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        ApplicantDetailVo vo = new ApplicantDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 申请人列新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(ApplicantCreateValidate createValidate) {
        Applicant model = new Applicant();
        model.setCustomerId(createValidate.getCustomerId());
        model.setCustomerName(createValidate.getCustomerName());
        model.setChangeRecordId(createValidate.getChangeRecordId());
        model.setCaseTypeId(createValidate.getCaseTypeId());
        model.setCaseType(createValidate.getCaseType());
        model.setApplicantTypeId(createValidate.getApplicantTypeId());
        model.setApplicantType(createValidate.getApplicantType());
        model.setEntityTypeId(createValidate.getEntityTypeId());
        model.setEntityType(createValidate.getEntityType());
        model.setCustomerNameChinese(createValidate.getCustomerNameChinese());
        model.setCustomerNameElse(createValidate.getCustomerNameElse());
        model.setIsFirstContact(createValidate.getIsFirstContact());
        model.setIsReceiptTitle(createValidate.getIsReceiptTitle());
        model.setPayTheFeesTitle(createValidate.getPayTheFeesTitle());
        model.setSocialCreditCode(createValidate.getSocialCreditCode());
        model.setCustomerNameEnglish(createValidate.getCustomerNameEnglish());
        model.setApplicantId(createValidate.getApplicantId());
        model.setApplicant(createValidate.getApplicant());
        model.setSurnameChinese(createValidate.getSurnameChinese());
        model.setNameChinese(createValidate.getNameChinese());
        model.setSurnameEnglish(createValidate.getSurnameEnglish());
        model.setNameEnglish(createValidate.getNameEnglish());
        model.setIsDefaultApplicant(createValidate.getIsDefaultApplicant());
        model.setHabitualResidence(createValidate.getHabitualResidence());
        model.setProvince(createValidate.getProvince());
        model.setCityChinese(createValidate.getCityChinese());
        model.setAdministrativeDivision(createValidate.getAdministrativeDivision());
        model.setPostcode(createValidate.getPostcode());
        model.setApplicantOfficialFee(createValidate.getApplicantOfficialFee());
        model.setCityEnglish(createValidate.getCityEnglish());
        model.setElectronicSubscriber(createValidate.getElectronicSubscriber());
        model.setStreetChinese(createValidate.getStreetChinese());
        model.setHistoricalAddressId(createValidate.getHistoricalAddressId());
        model.setStreetEnglish(createValidate.getStreetEnglish());
        model.setDepartmentChinese(createValidate.getDepartmentChinese());
        model.setDepartmentEnglish(createValidate.getDepartmentEnglish());
        model.setCertificateTypeId(createValidate.getCertificateTypeId());
        model.setCertificateType(createValidate.getCertificateType());
        model.setIdNumber(createValidate.getIdNumber());
        model.setFeeReductionFiling(createValidate.getFeeReductionFiling());
        model.setReductionCreateTime(createValidate.getReductionCreateTime());
        model.setReductionEndTime(createValidate.getReductionEndTime());
        model.setPowerOfAttorneyid(createValidate.getPowerOfAttorneyid());
        model.setRecordCertificateNumber(createValidate.getRecordCertificateNumber());
        model.setPctPowerOfAttorneyid(createValidate.getPctPowerOfAttorneyid());
        model.setContacts(createValidate.getContacts());
        model.setPhone(createValidate.getPhone());
        model.setFax(createValidate.getFax());
        model.setEmail(createValidate.getEmail());
        model.setNation(createValidate.getNation());
        model.setIsAnnualMonitoringFee(createValidate.getIsAnnualMonitoringFee());
        model.setAnnualMonitoringFee(createValidate.getAnnualMonitoringFee());
        model.setNationality(createValidate.getNationality());
        model.setBusinessLicense(createValidate.getBusinessLicense());
        model.setReductionFilename(createValidate.getReductionFilename());
        model.setReductionPath(createValidate.getReductionPath());
        model.setPowerOfAttorneyFilename(createValidate.getPowerOfAttorneyFilename());
        model.setPowerOfAttorneyPath(createValidate.getPowerOfAttorneyPath());
        model.setRemark(createValidate.getRemark());
        model.setApplicantRequestId(createValidate.getApplicantRequestId());
        model.setApplicantFileId(createValidate.getApplicantFileId());
        model.setCpcName(createValidate.getCpcName());
        model.setCpcPhone(createValidate.getCpcPhone());
        model.setCpcPostcode(createValidate.getCpcPostcode());
        model.setCpcEmail(createValidate.getCpcEmail());
        model.setCpcNation(createValidate.getCpcNation());
        model.setCpcProvince(createValidate.getCpcProvince());
        model.setCpcTown(createValidate.getCpcTown());
        model.setCpcCity(createValidate.getCpcCity());
        model.setCepctType(createValidate.getCepctType());
        model.setCepctNameChinese(createValidate.getCepctNameChinese());
        model.setCepctSurnameChinese(createValidate.getCepctSurnameChinese());
        model.setCepctNameEnglish(createValidate.getCepctNameEnglish());
        model.setCepctSurnameEnglish(createValidate.getCepctSurnameEnglish());
        model.setCepctNation(createValidate.getCepctNation());
        model.setCepctNationalProvince(createValidate.getCepctNationalProvince());
        model.setCepctCityChinese(createValidate.getCepctCityChinese());
        model.setCepctCityEnglish(createValidate.getCepctCityEnglish());
        model.setCepctStreetChinese(createValidate.getCepctStreetChinese());
        model.setCepctStreetEnglish(createValidate.getCepctStreetEnglish());
        model.setCepctPhone(createValidate.getCepctPhone());
        model.setCepctPostcode(createValidate.getCepctPostcode());
        model.setCepctFax(createValidate.getCepctFax());
        model.setCepctEmail(createValidate.getCepctEmail());
        model.setPoaName(createValidate.getPoaName());
        model.setPoaTitle(createValidate.getPoaTitle());
        model.setHagueName(createValidate.getHagueName());
        model.setHaguePhone(createValidate.getHaguePhone());
        model.setHaguePostcode(createValidate.getHaguePostcode());
        model.setHagueEmail(createValidate.getHagueEmail());
        model.setHagueNation(createValidate.getHagueNation());
        model.setHagueProvince(createValidate.getHagueProvince());
        model.setHagueTown(createValidate.getHagueTown());
        model.setHagueCity(createValidate.getHagueCity());
        model.setCreateTime(String.valueOf(System.currentTimeMillis() / 1000));
        applicantMapper.insert(model);
    }

    /**
     * 申请人列编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(ApplicantUpdateValidate updateValidate) {
        Applicant model = applicantMapper.selectOne(
                new QueryWrapper<Applicant>()
                    .eq("id",  updateValidate.getId())
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setCustomerId(updateValidate.getCustomerId());
        model.setCustomerName(updateValidate.getCustomerName());
        model.setChangeRecordId(updateValidate.getChangeRecordId());
        model.setCaseTypeId(updateValidate.getCaseTypeId());
        model.setCaseType(updateValidate.getCaseType());
        model.setApplicantTypeId(updateValidate.getApplicantTypeId());
        model.setApplicantType(updateValidate.getApplicantType());
        model.setEntityTypeId(updateValidate.getEntityTypeId());
        model.setEntityType(updateValidate.getEntityType());
        model.setCustomerNameChinese(updateValidate.getCustomerNameChinese());
        model.setCustomerNameElse(updateValidate.getCustomerNameElse());
        model.setIsFirstContact(updateValidate.getIsFirstContact());
        model.setIsReceiptTitle(updateValidate.getIsReceiptTitle());
        model.setPayTheFeesTitle(updateValidate.getPayTheFeesTitle());
        model.setSocialCreditCode(updateValidate.getSocialCreditCode());
        model.setCustomerNameEnglish(updateValidate.getCustomerNameEnglish());
        model.setApplicantId(updateValidate.getApplicantId());
        model.setApplicant(updateValidate.getApplicant());
        model.setSurnameChinese(updateValidate.getSurnameChinese());
        model.setNameChinese(updateValidate.getNameChinese());
        model.setSurnameEnglish(updateValidate.getSurnameEnglish());
        model.setNameEnglish(updateValidate.getNameEnglish());
        model.setIsDefaultApplicant(updateValidate.getIsDefaultApplicant());
        model.setHabitualResidence(updateValidate.getHabitualResidence());
        model.setProvince(updateValidate.getProvince());
        model.setCityChinese(updateValidate.getCityChinese());
        model.setAdministrativeDivision(updateValidate.getAdministrativeDivision());
        model.setPostcode(updateValidate.getPostcode());
        model.setApplicantOfficialFee(updateValidate.getApplicantOfficialFee());
        model.setCityEnglish(updateValidate.getCityEnglish());
        model.setElectronicSubscriber(updateValidate.getElectronicSubscriber());
        model.setStreetChinese(updateValidate.getStreetChinese());
        model.setHistoricalAddressId(updateValidate.getHistoricalAddressId());
        model.setStreetEnglish(updateValidate.getStreetEnglish());
        model.setDepartmentChinese(updateValidate.getDepartmentChinese());
        model.setDepartmentEnglish(updateValidate.getDepartmentEnglish());
        model.setCertificateTypeId(updateValidate.getCertificateTypeId());
        model.setCertificateType(updateValidate.getCertificateType());
        model.setIdNumber(updateValidate.getIdNumber());
        model.setFeeReductionFiling(updateValidate.getFeeReductionFiling());
        model.setReductionCreateTime(updateValidate.getReductionCreateTime());
        model.setReductionEndTime(updateValidate.getReductionEndTime());
        model.setPowerOfAttorneyid(updateValidate.getPowerOfAttorneyid());
        model.setRecordCertificateNumber(updateValidate.getRecordCertificateNumber());
        model.setPctPowerOfAttorneyid(updateValidate.getPctPowerOfAttorneyid());
        model.setContacts(updateValidate.getContacts());
        model.setPhone(updateValidate.getPhone());
        model.setFax(updateValidate.getFax());
        model.setEmail(updateValidate.getEmail());
        model.setNation(updateValidate.getNation());
        model.setIsAnnualMonitoringFee(updateValidate.getIsAnnualMonitoringFee());
        model.setAnnualMonitoringFee(updateValidate.getAnnualMonitoringFee());
        model.setNationality(updateValidate.getNationality());
        model.setBusinessLicense(updateValidate.getBusinessLicense());
        model.setReductionFilename(updateValidate.getReductionFilename());
        model.setReductionPath(updateValidate.getReductionPath());
        model.setPowerOfAttorneyFilename(updateValidate.getPowerOfAttorneyFilename());
        model.setPowerOfAttorneyPath(updateValidate.getPowerOfAttorneyPath());
        model.setRemark(updateValidate.getRemark());
        model.setApplicantRequestId(updateValidate.getApplicantRequestId());
        model.setApplicantFileId(updateValidate.getApplicantFileId());
        model.setCpcName(updateValidate.getCpcName());
        model.setCpcPhone(updateValidate.getCpcPhone());
        model.setCpcPostcode(updateValidate.getCpcPostcode());
        model.setCpcEmail(updateValidate.getCpcEmail());
        model.setCpcNation(updateValidate.getCpcNation());
        model.setCpcProvince(updateValidate.getCpcProvince());
        model.setCpcTown(updateValidate.getCpcTown());
        model.setCpcCity(updateValidate.getCpcCity());
        model.setCepctType(updateValidate.getCepctType());
        model.setCepctNameChinese(updateValidate.getCepctNameChinese());
        model.setCepctSurnameChinese(updateValidate.getCepctSurnameChinese());
        model.setCepctNameEnglish(updateValidate.getCepctNameEnglish());
        model.setCepctSurnameEnglish(updateValidate.getCepctSurnameEnglish());
        model.setCepctNation(updateValidate.getCepctNation());
        model.setCepctNationalProvince(updateValidate.getCepctNationalProvince());
        model.setCepctCityChinese(updateValidate.getCepctCityChinese());
        model.setCepctCityEnglish(updateValidate.getCepctCityEnglish());
        model.setCepctStreetChinese(updateValidate.getCepctStreetChinese());
        model.setCepctStreetEnglish(updateValidate.getCepctStreetEnglish());
        model.setCepctPhone(updateValidate.getCepctPhone());
        model.setCepctPostcode(updateValidate.getCepctPostcode());
        model.setCepctFax(updateValidate.getCepctFax());
        model.setCepctEmail(updateValidate.getCepctEmail());
        model.setPoaName(updateValidate.getPoaName());
        model.setPoaTitle(updateValidate.getPoaTitle());
        model.setHagueName(updateValidate.getHagueName());
        model.setHaguePhone(updateValidate.getHaguePhone());
        model.setHaguePostcode(updateValidate.getHaguePostcode());
        model.setHagueEmail(updateValidate.getHagueEmail());
        model.setHagueNation(updateValidate.getHagueNation());
        model.setHagueProvince(updateValidate.getHagueProvince());
        model.setHagueTown(updateValidate.getHagueTown());
        model.setHagueCity(updateValidate.getHagueCity());
        applicantMapper.updateById(model);
    }

    /**
     * 申请人列删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        Applicant model = applicantMapper.selectOne(
                new QueryWrapper<Applicant>()
                    .eq("id", id)
                    .eq("is_delete", 0)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setIsDelete(1);
        model.setDeleteTime(String.valueOf(System.currentTimeMillis() / 1000));
        applicantMapper.updateById(model);
    }

}
